package com.aSemy.qotwReader.core.question;

import java.io.*;
import java.net.*;

import net.htmlparser.jericho.*;

/**
 * The HTML source code of a question.
 * @author Adam Semenenko
 */
public class QuestionHTML
{
    /** The URL of the question. */
    final private URL questionURL;
    /** The HTML source code of the question using a Jericho Source object. */
    final private Source questionSource;
    
    /**
     * Creates a new object and downloads the HTML of the given URL.
     * @param questionURL The URL of the question to download.
     * @throws IOException If the download fails.
     */
    public QuestionHTML(URL questionURL) throws IOException
    {
        this.questionURL = questionURL;
        this.questionSource = downloadQuestionHTML();
    }
    
    /**
     * Downloads the HTML Source code of the question and holds it in a Source object (from Jericho).
     * @return The HTML Source
     * @throws IOException If the question could not be downloaded or converted to source by Jericho.
     */
    private Source downloadQuestionHTML() throws IOException
    {
        Source downloadedSource = new Source(questionURL);
        // Calling this method can greatly improve performance if most or all of the tags in the document need to be parsed.
        downloadedSource.fullSequentialParse();
        
        return downloadedSource;
    }
    
    /**
     * The Jericho Source object.
     * @return The HTML source code of the question.
     */
    public Source getQuestionSource() { return this.questionSource; }
}
